package cn.hniu.controller;

import cn.hniu.mapper.AdminMapper;
import cn.hniu.pojo.Admin;
import cn.hniu.pojo.Users;
import cn.hniu.pojo.util.Result;
import cn.hniu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;

    //登录功能
    @PostMapping("/login/{username}/{password}")
    public Result login(@PathVariable("username") String username, @PathVariable("password") String password){
        Admin admin=adminService.login(username,password);
        if(admin==null){
            return new Result(400,"账号或密码错误");
        }else {
            admin.setAd_state(1);
            adminMapper.updateById(admin);
            return new Result(200,admin);
        }
    }

    // 退出功能
    @PostMapping("/quit/{id}")
    public Result quit(@PathVariable("id") Integer id){
        Admin admin=new Admin();
        admin.setAd_id(id);
        admin.setAd_state(0);
        if(adminService.quit(id)==null){
            return new Result(400,"退出失败");
        }
        else {
            adminMapper.updateById(admin);
            return new Result(200,"退出成功");
        }
    }

}
